<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-goods"></i> 商城商品
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search.name" placeholder="商品名称" class="handle-input mr10" width="55" />
        <el-select class="handle-input mr10" v-model="search.plural" placeholder="商品分区" clearable @change="getData()">
          <el-option v-for="item in partitionList" :key="item.id" :label="item.pluralName" :value="item.id" />
        </el-select>
        <el-select class="handle-input mr10" v-model="search.commodityClassifyLevel1" placeholder="商品分类" clearable @change="getData()">
          <el-option v-for="item in classifyList" :key="item.id" :label="item.classifyName" :value="item.id" />
        </el-select>
        <el-select class="handle-input mr10" v-model="search.audit" placeholder="审核状态" clearable @change="getData()">
          <el-option v-for="item in auditStatusList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-input v-model="search.merchantName" placeholder="商户名称" class="handle-input mr10" width="55" />
        <el-date-picker v-model="search.time" type="datetime" placeholder="上架时间" @change="getData()" />

        <el-button type="primary" icon="el-icon-search" @click="getData()">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="showBox()">添加商品</el-button>
        <el-button type="primary" @click="handleAllUp()">一键上架</el-button>
        <el-button type="primary" @click="handleAllDown()">一键下架</el-button>

      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" :row-style="{ height: '80px' }" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="商品ID"></el-table-column>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="classifyPic" label="展示图片"> <template #default="scope"> <el-image class="table-td-thumb" :src="scope.row.showPic" fit="cover" /></template> </el-table-column>
        <el-table-column prop="commodityClassifyLevel1Name" label="商品分类"></el-table-column>
        <el-table-column prop="pluralName" label="商品分区"></el-table-column>
        <el-table-column prop="marketMoney" label="市场价"></el-table-column>
        <el-table-column prop="money" label="单价"></el-table-column>
        <el-table-column prop="merchantName" label="商户名称"></el-table-column>
        <el-table-column prop="status" label="商品状态" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.status == '0'" type="success">上架</el-tag>
            <el-tag v-else type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="auditTime" label="审核时间" :formatter="dateFormat"></el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template #default="scope">
            <el-button type="text" class="green" @click="handleCheak(scope.row)">查看</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleState(scope.row)">{{ scope.row.status == 0 ? "下架" : "上架" }}</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>

            <el-button type="text" v-if="scope.row.audit == 0" @click="handleAudit(scope.row)">审核</el-button>
            <el-button type="text" v-if="scope.row.audit == 1">已同意</el-button>
            <el-button type="text" v-if="scope.row.audit == 2">已拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex" :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="创建" v-model="showAddUser" width="70%">
      <el-form ref="form" :model="list" label-width="100px" :disabled="isDisabled">
        <el-row>
          <el-col :span="8"> <el-form-item label="商品名称"> <el-input v-model="list.name" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="商品分区">
              <el-select v-model="list.plural" placeholder="请选择" clearable>
                <el-option v-for="item in partitionList" :key="item.id" :label="item.pluralName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8"> <el-form-item label="商品一级分类">
              <el-select v-model="list.commodityClassifyLevel1" placeholder="请选择">
                <el-option v-for="item in classifyList" :key="item.id" :label="item.classifyName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8"> <el-form-item label="市场价"> <el-input v-model="list.marketMoney" type="number" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="单价"> <el-input v-model="list.money" type="number" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="抵扣金额"> <el-input v-model="list.money1" type="number" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="规格"> <el-input v-model="list.standard" placeholder="以 / 分割规格" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="尺码"> <el-input v-model="list.sizes" placeholder="以 / 分割尺码" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="型号"> <el-input v-model="list.model" placeholder="以 / 分割型号" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="库存"> <el-input v-model="list.stock" /> </el-form-item> </el-col>
          <el-col :span="8" v-if="isChange != 1"> <el-form-item label="销量"> <el-input v-model="list.sales" /> </el-form-item> </el-col>
          <!-- <el-col :span="8"> <el-form-item label="供应商"> <el-input v-model="list.supplier" /> </el-form-item> </el-col>-->
          <el-col :span="8"> <el-form-item label="商户手机号"> <el-input v-model="list.merchantMobile" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="赠送积分"> <el-input v-model="list.gainIntegral" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="利润"> <el-input v-model="list.profitMoney" /> </el-form-item> </el-col>
          <el-col :span="8"> <el-form-item label="贡献点"> <el-input v-model="list.gainValue" /> </el-form-item> </el-col>

          <el-col :span="8"> <el-form-item label="是否推荐">
              <el-radio-group v-model="list.isTj">
                <el-radio :label="1">推荐</el-radio>
                <el-radio :label="0">不推荐</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品图片">
              <div class="crop-demo">
                <img v-if="list.showPic" :src="list.showPic" class="pre-img" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" :disabled="isDisabled" />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="展示图片">
              <div class="crop">
                <div class="crop-demo">
                  <img v-if="list.showDetails1" :src="list.showDetails1" class="pre-img" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <input class="crop-input" type="file" name="image" accept="image/*" @change="upload_showDetails1" :disabled="isDisabled" />
                </div>
                <div class="crop-demo">
                  <img v-if="list.showDetails2" :src="list.showDetails2" class="pre-img" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <input class="crop-input" type="file" name="image" accept="image/*" @change="upload_showDetails2" :disabled="isDisabled" />
                </div>
                <div class="crop-demo">
                  <img v-if="list.showDetails3" :src="list.showDetails3" class="pre-img" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <input class="crop-input" type="file" name="image" accept="image/*" @change="upload_showDetails3" :disabled="isDisabled" />
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详情"> <Tinymce-editor :disabled="isDisabled" :value="list.contextDetails" @input="(res) => (list.contextDetails = res)" /> </el-form-item>
        <el-form-item label="说明"> <el-input type="textarea" placeholder="请输入内容" v-model="list.explains" show-word-limit :rows="12" resize="none" /> </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button v-if="isChange == 1" type="primary" @click="append">确 定</el-button>
          <el-button v-else type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 发货信息 -->
    <el-dialog title="发货信息" v-model="show_audit" width="30%">
      <el-form ref="form" label-width="100px">
        <el-form-item label="拒绝原因"> <el-input disabled v-model="form.auditReason" /> </el-form-item>
        <el-form-item label="赠送积分"> <el-input disabled v-model="form.gainIntegral" /> </el-form-item>
        <el-form-item label="推荐人积分"> <el-input v-model="form.gainShareIntegral" /> </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="confirm_audit(2)">拒绝</el-button>
          <el-button type="primary" @click="confirm_audit(1)">同意</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { commodityList, commodityUp, commodityEdit, commodityDel, commodityAdd, partitionList, } from "../../api/zyMall";
import { partitionDownList } from "../../api/zyMall";
import { classSon } from "../../api/zyMall";
import { ossUpload, api } from "../../api/index";
import { formatDate } from "../../common/data";
import TinymceEditor from "../../components/tinymce-editor";
import moment from "moment";
export default {
  inject: ["reload"],
  name: "basetable",
  components: { TinymceEditor },
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  data() {
    return {
      username: localStorage.getItem('ms_username'),
      query: { pageNo: 1 },
      search: {},
      tableData: [],
      showAddUser: false,
      pageTotal: 0,
      list: {},
      isChange: "",
      fileList: [],
      picList: [],
      isDisabled: false,
      dialogVisible: false,
      hideUploadCard: false,

      partitionList: [], // 分区列表
      classifyList: {}, // 分类列表
      auditStatusList: [
        { name: "待审核", id: 0 },
        { name: "已通过", id: 1 },
        { name: "已拒绝", id: 2 }
      ],
      // 审核弹窗
      show_audit: false,
      form: {},
      selectedList: []
    };
  },
  created() {
    this.getData();
    this.getFilter()
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) { return ""; }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    dataFilter(data) {
      if (data == undefined) { return ""; }
      return moment(data).format("YYYY-MM-DD");
    },
    joinpic(item) {
      return `http://xiuhaoshenghuo.xsiuyang.com/imgs/${item}`
    },
    // 获取分类列表
    getData() {
      console.log();
      commodityList({
        limit: 10,
        offset: this.query.pageNo,
        name: this.search.name,
        audit: this.search.audit,
        plural: this.search.plural,
        merchantName: this.search.merchantName,
        auditTime: this.dataFilter(this.search.time),
        commodityClassifyLevel1: this.search.commodityClassifyLevel1,
      }).then((res) => {
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },
    getFilter() {
      //获取分类列表
      api('/web/scc/pd', { classifyPid: 0 }).then((res) => { this.classifyList = res.data });
      //获取分区列表
      api('/web/sep/sl', {}).then((res) => { this.partitionList = res.data });

    },
    //显示编辑框
    showBox() {
      this.list = {}
      this.picList = []
      this.showAddUser = true;
      this.isChange = 1;
      this.isDisabled = false;
    },

    //创建分类
    append() {
      this.list.commodityClassifyLevel2 = 0;
      commodityAdd(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success("新增商品成功");
          this.list = {};
          this.getData();
          this.showAddUser = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //预览大图
    picPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //移除图片
    picRemove(file, fileList) {
      this.fileList = [];
      for (const i in fileList) {
        this.fileList.push(fileList[i].url);
        if (fileList[i].raw) {
          this.uploadFileMethod(fileList[i].raw);
        }
      }
    },
    //多图上传
    uploadFileMethod(param) {
      let formData = new FormData();
      if (param.file) {
        formData.append("file", param.file);
      } else {
        formData.append("file", param);
      }
      ossUpload(formData).then((res) => {
        this.fileList.push(res.data);
        this.hideUploadCard = this.fileList.length >= this.isType;
      });
    },
    //上传图片
    setImage(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      ossUpload(formData).then((res) => {
        this.list.showPic = res.data;
      });
    },
    upload_showDetails1(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      ossUpload(formData).then((res) => {
        this.list.showDetails1 = res.data;
      });
    },
    upload_showDetails2(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      ossUpload(formData).then((res) => {
        this.list.showDetails2 = res.data;
      });
    },
    upload_showDetails3(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      ossUpload(formData).then((res) => {
        this.list.showDetails3 = res.data;
      });
    },
    // 删除操作
    handleDelete(row) {
      this.$confirm("确定要删除吗？", "提示", { type: "warning" }).then(() => {
        commodityDel({ id: row.id, }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.getData();
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    //查看操作
    handleCheak(row) {
      this.picList = [];
      this.isDisabled = true;
      this.showAddUser = true;
      this.isChange = 0;
      this.list = row;
    },
    // 编辑操作
    handleEdit(row) {
      this.picList = [];
      this.list = row;
      this.isDisabled = false;
      this.showAddUser = true;
      this.isChange = 0;
    },
    //修改分类
    edit() {
      if (this.fileList.length == 0) {
        for (const i in this.picList) {
          this.fileList.push(this.picList[i].url);
        }
      }
      this.list.commodityClassifyLevel2 = 0;
      this.showAddUser = false;
      commodityEdit(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success("修改商品成功");
          this.list = {};
          this.getData();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //上下架
    handleState(row) {
      this.$confirm("确定改变状态", "提示", { type: "warning", }).then(() => {
        commodityUp({ id: row.id, status: row.status == 0 ? "1" : "0", }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.getData();
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },

    handleSelectionChange(val) {
      console.log(val.map(item => item.id));
      this.selectedList = val.map(item => item.id);
    },

    handleAllUp(row) {
      this.$confirm("确定改变状态", "提示", { type: "warning", }).then(() => {
        api('/web/sc/ocr', { commodityClassifyLevel1: this.search.commodityClassifyLevel1, status: 0, }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.getData();
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    handleAllDown(row) {
      this.$confirm("确定改变状态", "提示", { type: "warning", }).then(() => {
        api('/web/sc/ocr', { commodityClassifyLevel1: this.search.commodityClassifyLevel1, status: 1, }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.getData();
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    handleAudit(row) {
      this.form = row;
      this.show_audit = true
    },
    confirm_audit(item) {
      api('/web/sc/audit', {
        audit: item,
        auditReason: this.form.auditReason,
        gainIntegral: this.form.gainIntegral,
        gainShareIntegral: this.form.gainShareIntegral,
        id: this.form.id,
      }).then((res) => {
        if (res.code === 2000) {
          this.$message.success(res.message);
          this.getData();
        } else {
          this.$message.error(res.message);
        }
      });
    },

    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>
<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>
<style lang='scss' scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.green {
  color: #0ab618;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  height: 60px;
}

.crop {
  display: flex;
  align-items: center;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 148px;
  height: 148px;
  margin-right: 5px;
  position: relative;

  .pre-img {
    width: 146px;
    height: 146px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 148px;
    height: 148px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
}
</style>
